package cn.rentaotao.spring.websocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author rtt
 * @date 2022/5/25 14:13
 */
// @Service
public class ShoutServiceImpl implements ShoutService{

    private final SimpMessageSendingOperations messageSendingOperations;

    // @Autowired
    public ShoutServiceImpl(SimpMessageSendingOperations messageSendingOperations) {
        this.messageSendingOperations = messageSendingOperations;
    }

    @Override
    public Shout get(String arg) {
        Shout shout = new Shout();
        shout.setMessage("return message " + arg);
        return shout;
    }

    @Override
    // @Scheduled(fixedDelay = 10000)
    public void sendShout() {
        System.out.println("--- 发送更新信息");
        messageSendingOperations.convertAndSend("/topic/shout", get(new Date().toString()));
    }
}
